﻿using Blm.Utils.Extensions;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Options;
using System;
using System.IO;
using System.Threading.Tasks;
using VisionCloud.Domain.Common;
using VisionCloud.Utility;
using VisionCloud.Utility.Option;
using Path = System.IO.Path;

namespace VisionCloud.Service.Ms
{
    public class MsLogService
    {
        private UploadFileOptions _uploadFileOptions;

        public MsLogService(IOptionsMonitor<UploadFileOptions> option)
        {
            if (option.IsNull())
                throw new ArgumentNullException($"{typeof(UploadFileOptions)} was null");
            _uploadFileOptions = option.CurrentValue;
        }

        public async Task<ApiResult> UploadLog(string deviceGuid, string type, IFormFile formFile)
        {
            if (deviceGuid.ToGuid() == Guid.Empty)
                return new ApiResult(AjaxCode.Error, "YB0059".SLocalize());

            var path = Path.Combine(_uploadFileOptions.ParentDir, _uploadFileOptions.LogDir, deviceGuid, type);
            if (!Directory.Exists(path)) Directory.CreateDirectory(path);
            using (var fileStream = new FileStream(Path.Combine(path, formFile.FileName), FileMode.Create))
            {
                await formFile.CopyToAsync(fileStream);
            }
            return ApiResult.Success();
        }
    }
}